<template>
  <div class="out">
    <div>
      <el-button type="primary" size="default" @click="addCircle"
        >增加标记点</el-button
      >
      {{ activeOne }}
    </div>

    <div class="con">
      <div class="canvas">
        <img class="basePic" src="./bg.png" alt="" />
        <canvas id="cvs"></canvas>
        <!-- 圆圈点 -->
        <vue-draggable-resizable
          v-for="(item, idx) in listCircle"
          :key="idx + item.w"
          :w="item.w"
          :h="item.h"
          :x="item.x"
          :y="item.y"
          :parent="true"
          :grid="[10, 10]"
          :resizable="false"
          :draggable="!item.lock"
          :z-index="99"
          style="z-index: 99"
          @dragging="(x, y) => onDrag(x, y, item)"
          @resizing="onResize"
        >
          <div
            class="itemCircle"
            :class="{ active: item.active, lock: item.lock }"
            @click="clickCircle(item, idx)"
            v-clickoutside="clickoutside"
          ></div>
        </vue-draggable-resizable>

        <!-- 描述图标 -->
        <vue-draggable-resizable
          v-for="item in listIcon"
          :key="item.id"
          :w="item.iW"
          :h="item.iH"
          :x="item.iX"
          :y="item.iY"
          :parent="true"
          :grid="[10, 10]"
          :resizable="false"
          :z-index="99"
          style="z-index: 99"
          @dragging="(x, y) => onDragIcon(x, y, item)"
          @dragstop="onDragStop(item.id)"
        >
          <span class="icon" @click="clickItem(item)">
            {{ item.name }}
          </span>
        </vue-draggable-resizable>
      </div>
      <div v-if="adtiveIdx > -1" class="form">
        <div>circle: x: {{ activeOne.x }},y: {{ activeOne.y }}</div>
        <div>icon: x: {{ activeOne.iX }},y: {{ activeOne.iY }}</div>
        <el-form ref="form" :model="form" label-width="100px">
          <el-form-item label="名称">
            <el-input v-model="form.name" @change="formChange"></el-input>
          </el-form-item>
          <el-form-item label="其他属性">
            <el-input v-model="form.other" @change="formChange"></el-input>
          </el-form-item>
          <el-form-item label="锁定点位位置">
            <el-switch v-model="form.lock" @change="formChange"></el-switch>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" :disabled="!activeOne" @click="onSubmit"
              >配置图标</el-button
            >
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>
import VueDraggableResizable from "vue-draggable-resizable";
import "vue-draggable-resizable/dist/VueDraggableResizable.css";

import { uniqueId } from "lodash";

export default {
  components: {
    VueDraggableResizable,
  },
  name: "HelloWorld",
  data: function () {
    return {
      listCircle: [],
      form: {
        name: "",
        other: "",
        lock: false,
      },
      activeId: null,
    };
  },
  computed: {
    activeOne() {
      return this.listCircle.find((f) => f.active);
    },
    adtiveIdx() {
      return this.listCircle.findIndex((f) => f.active);
    },
    listIcon() {
      return this.listCircle.filter((f) => f.showIcon);
    },
  },
  methods: {
    clickItem(item) {
      console.log(111, item);
    },
    // eslint-disable-next-line no-unused-vars
    onResize: function (x, y, width, height) {
      // this.x = x
      // this.y = y
      // this.width = width
      // this.height = height
    },
    onDrag: function (x, y, item) {
      item.x = x;
      item.y = y;
      this.drawLine();
    },
    addCircle() {
      let obj = {
        id: uniqueId(),
        w: 6,
        h: 6,
        x: 10 * this.listCircle.length + 10,
        y: 10,
      };
      this.listCircle.push(obj);
    },
    clickCircle(item, idx) {
      this.form = {
        name: item.name,
        other: item.other,
        lock: item.lock,
      };

      this.listCircle = this.listCircle.map((m, i) => ({
        ...m,
        active: idx === i && !m.active,
      }));
    },
    clickoutside() {
      // this.listCircle = this.listCircle.map((m) => ({
      //   ...m,
      //   active: false,
      // }))
    },
    onSubmit() {
      this.$set(this.listCircle, this.adtiveIdx, {
        ...this.listCircle[this.adtiveIdx],
        showIcon: true,
        iW: 50,
        iH: 30,
        iX: 40,
        iY: 40,
      });
    },

    formChange() {
      this.$set(this.listCircle, this.adtiveIdx, {
        ...this.listCircle[this.adtiveIdx],
        ...this.form,
      });
    },
    onDragIcon(x, y, { id }) {
      for (const item of this.listCircle) {
        if (item.id === id) {
          item.iX = x;
          item.iY = y;
          break;
        }
      }

      this.listCircle = [...this.listCircle];
      this.drawLine();
    },
    onDragStop(id) {
      this.activeId = id;
      this.drawLine();
    },

    drawLine() {
      const canvas = document.querySelector("#cvs");
      if (!canvas.getContext("2d")) {
        return this.$message.warning(
          "当前浏览器不支持canvas，建议使用谷歌浏览器再试!"
        );
      }
      canvas.width = 500;
      canvas.height = 400;
      const ctx = canvas.getContext("2d"); //3d为webgl那就很有深度的了；

      this.listIcon.forEach((item) => {
        ctx.moveTo(item.x, item.y); //移动到某个点；
        ctx.lineTo(item.iX + 30, item.iY); //画一条到某个地方的线条
        ctx.closePath(); //闭合；
      });

      ctx.lineWidth = "2"; //设置线条的宽度；不能加单位
      ctx.strokeStyle = "blue"; //rgba(0,222,255,.5)
      ctx.stroke(); //描绘；描边；这一步让线条有了颜色；默认黑色
      ctx.fillStyle = "red";
      ctx.fill();
    },

    test() {
      this.drawLine();
    },
  },
};
</script>

<style>
.out {
  width: 100%;
  text-align: left;
}
.con {
  width: 100%;
  display: flex;
  margin-top: 20px;
  position: relative;
}

.basePic {
  width: 100%;
  position: absolute;
  z-index: 1;
  top: 0;
  bottom: 0;
  margin: auto;
}
.canvas {
  box-sizing: border-box;
  width: 500px;
  height: 400px;
  border: 1px solid #000;
  position: relative;
}
.form {
  margin-left: 20px;
}
#cvs {
  width: 100%;
  height: 100%;
  position: absolute;
  top: 0;
  left: 0;
  background-color: rgba(0, 0, 0, 0.2);
  z-index: 2;
}

.item {
  border: 1px solid #000;
  color: #000;
}
.itemCircle {
  width: 6px;
  height: 6px;
  border: 1px solid #000;
  border-radius: 3px;
}
.itemCircle.active {
  background-color: aquamarine;
}
.itemCircle.lock {
  background-color: #000;
}
.icon {
  display: inline;
  width: 45px;

  background-color: #00b1f1;
  padding: 5px 10px;
  border-radius: 11px;
}
.vdr {
  border: none;
}

.draggable {
  width: 100px;
}
</style>
